﻿using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Util;
using Kingdee.K3.SCM.App.Sal.Report;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.ReportPlugin.YePeng
{
    [Description("YP:销售订单执行明细表_扩展"), HotUpdate]
    public class SalDetailRptEX : SalDetailRpt
    {
        private String[] customRptTempTableNames;
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            IDBService dBService = ServiceHelper.GetService<IDBService>();
            customRptTempTableNames = dBService.CreateTemporaryTableName(this.Context, 1);
            String strTable = customRptTempTableNames[0];
            base.BuilderReportSqlAndTempTable(filter, strTable);
            StringBuilder sb = new StringBuilder();
            string strSql = @"/*dialect*/select t1.*,T2.F_YPYP_YTZZDQTY,t2.F_YPYP_YTWZZDQTY,t2.F_YPYP_IFONETIMEPROD,t2.F_YPJX_XSDDHTH,t2.F_YPYP_CUSTNUMBER   
            into {0}  FROM {1} t1 
            left join (select t0.FENTRYID,t0.F_YPYP_YTZZDQTY,t0.F_YPYP_YTWZZDQTY,case when t1.F_YPYP_IFONETIMEPROD=1 then '是' else '否' end F_YPYP_IFONETIMEPROD,t0.F_YPJX_XSDDHTH,t2.F_YPYP_CUSTNUMBER
            from T_SAL_ORDERENTRY t0
            join T_SAL_ORDER t1 on t1.FID=t0.FID
            join T_BD_MATERIAL t2 on t2.FMATERIALID=t0.FMATERIALID) t2 on t2.FENTRYID=t1.FORDERID 
            ";
            sb.AppendFormat(strSql, tableName, strTable);
            DBUtils.Execute(this.Context, sb.ToString());
        }
        //销售订单执行明细表，如下方法添加显示不了报表标题，需要同时在BOS平台，在对应过滤框的显示隐藏列里添加，字段名和SQL的名字要一样
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            ReportHeader header = base.GetReportHeaders(filter);
            header.AddChild("F_YPYP_YTZZDQTY", new LocaleValue("预投转正单数量"));
            header.AddChild("F_YPYP_YTWZZDQTY", new LocaleValue("预投未转正单数量"));
            header.AddChild("F_YPYP_IFONETIMEPROD", new LocaleValue("是否一次性订单"));
            header.AddChild("F_YPJX_XSDDHTH", new LocaleValue("客户合同号"));
            header.AddChild("F_YPYP_CUSTNUMBER", new LocaleValue("客户物料号"));
            return header;
        }
        public override void CloseReport()
        {
            //删除临时表
            if (customRptTempTableNames.IsNullOrEmptyOrWhiteSpace())
            {
                return;
            }
            IDBService dbService = Kingdee.BOS.App.ServiceHelper.GetService<Kingdee.BOS.Contracts.IDBService>();
            dbService.DeleteTemporaryTableName(this.Context, customRptTempTableNames);
            base.CloseReport();
        }
    }
}
